package com.junzhiit.nengji.api.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.api.service.GoodsService;
import com.junzhiit.nengji.api.service.ShopService;
import com.junzhiit.nengji.api.web.vo.ListVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.api.web.vo.ShopVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController {

	@Autowired
	private ShopService shopService;
	
	@Autowired
	private GoodsService goodsService;

	@ApiOperation(value = "根据shopID查询店铺和店铺商品信息")
	@RequestMapping(value = "/{shopId}", method = RequestMethod.GET)
	public ResultVO getShopById(Integer pageNumber,Integer maxPageRow,String userId,@PathVariable String shopId,Integer status,Integer goodsSearchType) {
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		
		ResultVO result = new ResultVO();
		Map<String,Object> map=new HashMap<String,Object>();
		
		ShopVO shopVO=shopService.getById(userId,shopId);
		ListVO listVO=goodsService.findAll(userId,shopId,status,goodsSearchType,pageNumber,maxPageRow);
		if(shopVO!=null){
			map.put("shopInfo", shopVO);
		}
		if(listVO!=null){
			map.put("goodsList", listVO);
		}
		
		result.setData(map);
		return result;
	}
	
	@ApiOperation(value = "查询店铺详细信息")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public ResultVO findShopById(String userId,String shopId) {
		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		ResultVO result=new ResultVO();
		result.setData(shopService.getById(userId, shopId));
		return result;
	}
	
	@ApiOperation(value = "分享店铺成功，新增数据")
	@RequestMapping(value = "/history/add", method = RequestMethod.POST)
	public ResultVO addShopHistory(String userId,String shopId,Integer type) {
		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		ResultVO result=new ResultVO();
		result.setData(shopService.addShopHistory(shopId, userId, type));
		return result;
	}
}
